<template>
  <div class="cart" v-loading="loading" element-loading-text="拼命加载中">
    <!-- <div class="mask" v-show="isLoading"></div> -->
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">单价（元）</div>
        <div class="cart-th4">数量</div>
        <div class="cart-th5">小计（元）</div>
        <div class="cart-th6">操作</div>
      </div>
      <div class="cart-body">
        <ul class="cart-list" v-for="good in cartList" :key="good.id">
          <li class="cart-list-con1">
            <input
              type="checkbox"
              name="chk_list"
              :checked="good.isChecked === 1"
              @change="changeSingle(good.skuId, good.isChecked === 1 ? 0 : 1)"
            />
          </li>
          <li class="cart-list-con2">
            <img :src="good.imgUrl" />
            <div class="item-msg">
              {{ good.skuName }}
            </div>
          </li>
          <li class="cart-list-con3">
            <div class="item-txt">语音升级款</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">{{ good.skuPrice }}.00</span>
          </li>
          <li class="cart-list-con5">
            <a
              href="javascript:void(0)"
              class="mins"
              @click="changeNum(good, -1)"
              >-</a
            >
            <input
              autocomplete="off"
              type="text"
              :value="good.skuNum"
              minnum="1"
              class="itxt"
              @change="changeIptNum(good, $event)"
            />
            <a
              href="javascript:void(0)"
              class="plus"
              @click="changeNum(good, 1)"
              >+</a
            >
          </li>
          <li class="cart-list-con6">
            <span class="sum">{{ good.skuNum * good.skuPrice }}</span>
          </li>
          <li class="cart-list-con7">
            <a class="sindelet" @click="deleteSingle(good.skuId)">删除</a>
            <br />
            <a href="#none">移到收藏</a>
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" v-model="allChecked" />
        <span>全选</span>
      </div>
      <div class="option">
        <a @click="deleteAll">删除选中的商品</a>
        <a>移到我的关注</a>
        <a>清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">
          已选择 <span>{{ totalNum }}</span
          >件商品
        </div>
        <div class="sumprice">
          <em>总价（不含运费） ：</em>
          <i class="summoney">{{ totalPrice }}</i>
        </div>
        <div class="sumbtn">
          <router-link to="/trade">结算</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  reqShopCart,
  reqCheckSingle,
  reqCheckAll,
  reqDeleteSingle,
  reqDeleteAll,
  reqChangeCartNum,
} from "@/api/shopCart";
export default {
  name: "ShopCart",
  data() {
    return {
      cartList: [],
      isReq: false,
      // isLoading: false,
      loading: false,
    };
  },
  mounted() {
    this.getShopCart();
  },
  methods: {
    //1.初始化请求购物车数据
    async getShopCart() {
      this.loading = true;

      const result = await reqShopCart();
      //给请求回来的数据都添加一个isReq属性,代表当前是否正在数量改变的请求中
      //因为本身修改的就不是vue上的数据(此时还没有赋值,所以不需要使用$set)
      this.cartList =
        result[0]?.cartInfoList.map((item) => {
          item.isReq = false;
          return item;
        }) || [];
      this.loading = false;
    },

    //2. 切换单个选中的状态
    async changeSingle(skuId, isChecked) {
      this.loading = true;
      await reqCheckSingle(skuId, isChecked);

      //重新发送请求,得到最新的数据渲染
      this.getShopCart();
    },

    //3. 删除单个
    async deleteSingle(skuId) {
      this.loading = true;

      await reqDeleteSingle(skuId);

      //重新发送请求,得到最新的数据渲染
      this.getShopCart();
    },

    //4. 删除选中商品
    async deleteAll() {
      this.loading = true;

      /* this.cartList.reduce((p, c) => {
        // if (c.isChecked === 1) {
        //   p.push(c.skuId);
        // }
        // return p;

        return c.isChecked === 1 ? [...p, c.skuId] : p;
      }, []); */

      const skuIdList = this.cartList.reduce(
        (p, c) => (c.isChecked === 1 ? [...p, c.skuId] : p),
        []
      );

      await reqDeleteAll(skuIdList);

      //重新发送请求,得到最新的数据渲染
      this.getShopCart();
    },

    //5. 对商品数量的加减
    async changeNum(good, num) {
      //每次触发事件的时候,判断当前是否在请求中,如果还在请求中,则直接return出去,不让继续修改数量
      if (good.isReq) return;

      //上边的判断放行(不再请求中)了以后,才能执行后边的代码,此时我们应该立即再把状态设置为正在请求中状态
      good.isReq = true;
      //如果商品的数量已经小于等于1了， 并且现在是减法,则不再进行数量的改动
      //一旦不符合条件,也要把状态改为 未请求
      if (good.skuNum <= 1 && num === -1) return (good.isReq = false);

      this.loading = true;

      await reqChangeCartNum(good.skuId, num);

      //重新发送请求,得到最新的数据渲染
      await this.getShopCart();

      //等请求完成,则立即把状态设置为 没有请求中状态
      good.isReq = false;
    },

    //6. 对商品数量输入的改动
    async changeIptNum(good, e) {
      const reg = /^[0-9]+$/g;
      if (!reg.test(e.target.value)) return (e.target.value = good.skuNum);

      this.loading = true;

      //发送修改请求
      await reqChangeCartNum(good.skuId, e.target.value - good.skuNum);

      //重新发送请求,得到最新的数据渲染
      this.getShopCart();
    },
  },
  computed: {
    totalNum() {
      return this.cartList.reduce((p, c) => p + c.skuNum, 0);
    },
    totalPrice() {
      return this.cartList.reduce((p, c) => p + c.skuNum * c.skuPrice, 0);
    },
    allChecked: {
      get() {
        return this.cartList.every((item) => item.isChecked === 1);
      },
      async set(value) {
        this.loading = true;

        const newStatus = value ? 1 : 0;

        const skuIdList = this.cartList.map((item) => item.skuId);

        await reqCheckAll(newStatus, skuIdList);

        //重新发送请求,得到最新的数据渲染
        this.getShopCart();
      },
    },
  },
};
</script>

<style lang="less" scoped>
body {
  margin: 0;
}
.cart {
  width: 1200px;
  margin: 0 auto;
  position: relative;
  .mask {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    background: url("https://img.zcool.cn/community/0177aa60ac8ffe11013f47208b2584.gif")
      center center no-repeat;
    background-size: 100% 100%;
    opacity: 0.5;
  }

  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }

  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;

      & > div {
        float: left;
      }

      .cart-th1 {
        width: 25%;

        input {
          vertical-align: middle;
        }

        span {
          vertical-align: middle;
        }
      }

      .cart-th2 {
        width: 25%;
      }

      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6 {
        width: 12.5%;
      }
    }

    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;

      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;

        & > li {
          float: left;
        }

        .cart-list-con1 {
          width: 4.1667%;
        }

        .cart-list-con2 {
          width: 25%;

          img {
            width: 82px;
            height: 82px;
            float: left;
          }

          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }

        .cart-list-con3 {
          width: 20.8333%;

          .item-txt {
            text-align: center;
          }
        }

        .cart-list-con4 {
          width: 12.5%;
        }

        .cart-list-con5 {
          width: 12.5%;

          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }

          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 31px;
            float: left;
            text-align: center;
            font-size: 14px;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
        }

        .cart-list-con6 {
          width: 12.5%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 12.5%;

          a {
            color: #666;
          }
        }
      }
    }
  }

  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: "Microsoft YaHei";
          background: #e1251b;
          overflow: hidden;
        }
      }
    }
  }
}
</style>
